package yf.mall.dao;

import com.yf.dao.BaseDao;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import yf.mall.entity.UserEntity;
import yf.mall.vo.UserVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import yf.mall.vo.web.UserVO;
import yf.mall.vo.web.VipUserVO;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 用户Dao
 *
 * 
 * 
 * @date Tue Aug 27 18:12:02 CST 2019
 */
@Mapper
public interface UserDao extends BaseDao<UserEntity> {
    List<UserVo> queryListVo(UserVo userVo);

    UserVo getVo(Long id);

    @Select("select id,name,code,level_id,pid from user where  id != 0 and pid is not null")
    List<UserEntity> findAll();

    UserEntity findByCode(String code);

    @Select("select id from user where  user.mobile = #{code} or user.code = #{code}")
    UserEntity findByAny(String code);

    UserEntity findByMobile(String mobile);

    List<VipUserVO> findByVip(String tCode);

    List<UserEntity> findByLevelId(Long levelId);

    //找到比级别小的
    List<UserEntity> findByGtLv(Integer lv);

    //找到比级别大的
    List<UserEntity> findByLtLv(Integer lv);

    //通过微信OpenId或者UnionID查找
    UserEntity findByWxId(String wxId);

    //替换团队
    void updateTeam(@Param("oldTCode") String oldTCode, @Param("nowTCode") String nowTCode, @Param("oldTeam") String oldTeam, @Param("nowTeam") String nowTeam, @Param("lv") Integer lv);

    void updateTeamBatch(@Param("oldTCode") String oldTCode, @Param("nowTCode") String nowTCode,List<UserEntity> users);

    List<UserVo> findByNameOrCodeOrMobile(@Param("param") String param);

    List<UserEntity> findByTops(@Param("userIds") List<Long> userIds);


    List<UserEntity> findByTop(@Param("userId") Long userId);

    List<UserEntity> findByList(@Param("users") List<UserEntity> users);


    Integer findFansNum(@Param("userId") Long userId, @Param("lv") int lv);

    Integer findAgentNum(@Param("userId") Long userId, @Param("lv") int lv);

    List<UserVo> findByUserId(long userId);


    List<UserVo> findList(@Param("userVos") List<UserVo> userVos);

    Integer sumUserByPidAndLevel(@Param("userId") Long userId, @Param("levelId") Long levelId);

    @Select("select user.* from user left JOIN `level` on `user`.level_id = `level`.id WHERE lv < #{lv}")
    List<UserEntity> listUserLessThan(Integer lv);

    List<UserEntity> findByPercent();

    @Select("select id,level_id from user where  pid = #{userId}")
    List<UserEntity> findByPid(Long userId);

    @Select("select  id ,name ,code,password,avatar,level_id ,mobile,time,zs_vip as zs from user where id = #{userId}")
    UserVO findVO(Long userId);

    @Select("select  id ,name ,code,password,avatar,level_id ,mobile,zs_vip as zs from user where pid = #{userId}")
    UserVO findVOByPid(Long userId);

    @Select("select  id ,name ,code,password,avatar,level_id ,time,mobile,zs_vip as zs from user where code = #{code} or mobile = #{code}")
    UserVO findVOByAny(String code);


    List<UserVO> findVOByTops(@Param("userIds") List<Long> userIds);


    @Select("select  id ,name ,code,password,avatar,level_id ,mobile,zs_vip as zs from user where open_id = #{wxId} or union_id = #{wxId}}")
    List<UserVO> findVOByWxId(String wxId);

    @Select("select * from user left join level_record as lr on lr.user_id = user.id and lr.id = (selct max(id) from level_record  where user.id = user_id )) where DATE_FORMAT(lr.time,'%Y-%m')  = DATE_FORMAT(#{date},'%Y-%m')")
    List<UserEntity> findByMonth(Date date);

    @Update("update user set open_id =null ,union_id = null,avatar = null where id = #{userId}")
    void resetWxUser(Long userId);


    List<UserEntity> findByTeam(@Param("team") String team, @Param("lv") Integer lv);

    void updateFidBatch(@Param("fid") Long fid, @Param("userIds") List<Long> userIds);

    void updateRidBatch(@Param("rid") Long fid, @Param("userIds") List<Long> userIds);

    List<UserEntity> finByUserIds(@Param("userIds") Set<Long> userIds);

    @Select("select open_id from  user WHERE open_id is not  NULL and open_id !=''")
    List<String> findAllOpenId();
}
